package com.springboot.service.blog;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springboot.dao.BlogArticleDao;
import com.springboot.dao.BlogArticleTagDao;
import com.springboot.entity.BlogArticleEntity;
import com.springboot.entity.BlogArticleTagEntity;
import com.springboot.file.BaseFileStoreFileUtil;
import com.springboot.file.FileStoreServiceUtil;
import com.springboot.file.filedto.MultipartFileDto;
import com.springboot.returndata.dto.blog.BlogArticlePageDto;
import com.springboot.returndata.dto.blog.BlogArticleUpAdDto;
import com.springboot.returndata.dto.blog.UpdateBlogArticleStatusDto;
import com.springboot.returndata.vo.blog.BlogArticleInfoVo;
import com.springboot.returndata.vo.blog.BlogArticleVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import jakarta.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 博客文章内容表 服务类
 * </p>
 *
 * @author zyj
 * @since 2023-03-23
 */
@Service
public class BlogArticleService extends ServiceImpl<BlogArticleDao, BlogArticleEntity> {

    @Resource
    private BlogArticleDao thisMapper;

    @Resource
    private BaseFileStoreFileUtil ftpUtils;

    @Resource
    private BlogArticleTagDao blogArticleTagDao;


    @Resource
    private FileStoreServiceUtil fileStoreServiceUtil;

    @Resource
    private BaseFileStoreFileUtil baseFileStoreFileUtil;

    /**
     * 获取文章分类列表
     *
     * @param blogArticlePageDto
     * @return
     */
    public Page<BlogArticleVo> pageBlogArticle(BlogArticlePageDto blogArticlePageDto) {
        Page<BlogArticleVo> roleList = thisMapper.pageBlogArticle(blogArticlePageDto.getPage(), blogArticlePageDto);
        return roleList;
    }

    /**
     * 方法描述 修改文章状态
     * @since: 1.0
     * @param updateBlogArticleStatusDto
     * @return: void
     * @author: zyj
     * @date: 2023/6/1 10:41
     */
    public void updateBlogArticleStatus(UpdateBlogArticleStatusDto updateBlogArticleStatusDto){
        thisMapper.updateStatus(updateBlogArticleStatusDto.getId(), updateBlogArticleStatusDto.getArticleStatus());
    }

    /**
     * 方法描述 保存文章
     *
     * @param blogArticleUpAdDto
     * @since: 1.0
     * @return: void
     * @author: zyj
     * @date: 2023/3/29 14:48
     */
    @Transactional(rollbackFor = Exception.class)
    public void addBlogArticle(BlogArticleUpAdDto blogArticleUpAdDto) throws Exception {
        BlogArticleEntity blogArticleEntity = new BlogArticleEntity();
        BeanUtils.copyProperties(blogArticleUpAdDto, blogArticleEntity);
        /**转换内容中的图片地址*/
        blogArticleUpAdDto.setArticleContent(fileStoreServiceUtil.replaceUrlUnsignedUrl(blogArticleUpAdDto.getArticleContent()));
        thisMapper.insert(blogArticleEntity);

        String articleCloudTag = blogArticleUpAdDto.getSelect();
        if (!StringUtils.isEmpty(articleCloudTag)) {
            String[] String = articleCloudTag.split(",");
            for (String tagId : String) {
                BlogArticleTagEntity blogArticleTagEntity = new BlogArticleTagEntity();
                blogArticleTagEntity.setArticleId(blogArticleEntity.getId());
                blogArticleTagEntity.setTagId(Integer.parseInt(tagId));
                blogArticleTagDao.insert(blogArticleTagEntity);
            }
        }

    }

    /**
     * 更新文章
     * @param blogArticleUpAdDto
     * @throws Exception
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateBlogArticle(BlogArticleUpAdDto blogArticleUpAdDto) throws Exception {
        BlogArticleEntity blogArticleEntity = new BlogArticleEntity();
        BeanUtils.copyProperties(blogArticleUpAdDto, blogArticleEntity);
        /**转换内容中的图片地址*/
        blogArticleEntity.setArticleContent(fileStoreServiceUtil.replaceUrlUnsignedUrl(blogArticleUpAdDto.getArticleContent()));
        thisMapper.updateById(blogArticleEntity);

        blogArticleTagDao.deleteByArticleId(blogArticleEntity.getId());
        String articleCloudTag = blogArticleUpAdDto.getSelect();
        if (!StringUtils.isEmpty(articleCloudTag)) {
            String[] String = articleCloudTag.split(",");
            for (String tagId : String) {
                BlogArticleTagEntity blogArticleTagEntity = new BlogArticleTagEntity();
                blogArticleTagEntity.setArticleId(blogArticleEntity.getId());
                blogArticleTagEntity.setTagId(Integer.parseInt(tagId));
                blogArticleTagDao.insert(blogArticleTagEntity);
            }
        }

    }

    @Transactional(rollbackFor = Exception.class)
    public void deleteBlogArticle(Integer id){
        thisMapper.deleteById(id);

        blogArticleTagDao.deleteByArticleId(id);
    }

    /**
     * 方法描述
     * @since: 1.0
     * @param id
     * @return: BlogArticleEntity
     * @author: zyj
     * @date: 2023/3/30 19:18
     */
    public BlogArticleInfoVo getBlogArticleInfo(Integer id) throws Exception{
        BlogArticleInfoVo blogArticleVo = thisMapper.findBlogArticleById(id);
        blogArticleVo.setImg(blogArticleVo.getArticleImg());
        blogArticleVo.setImgShow(baseFileStoreFileUtil.getFileLink(blogArticleVo.getArticleImg()));
        return blogArticleVo;
    }

    /**
     * 文件单独上传
     * @param multipartFiles
     * @return
     */
    public Map<String,String> uploadImgBlog(MultipartFile[] multipartFiles) throws Exception{
        Map<String, String> result = new HashMap<>();
        MultipartFileDto multipartFileDto = new MultipartFileDto(multipartFiles[0], BlogSortService.blogFilePath);
        String fileToFtp = ftpUtils.uploadMultipartFile(multipartFileDto);
        String articleImg = ftpUtils.getFileLink(fileToFtp);
        result.put("imgShow",articleImg);
        result.put("img", fileToFtp);
        return result;
    }
}
